package com.gzhryc.net_marketing.cloud.micro.api.controllers.v1;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

import com.gzhryc.common.ClassTools;
import com.gzhryc.common.StringTools;
import com.gzhryc.common.data.ValidateTools;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.net_marketing.cloud.micro.api.ClientAPICodeConstants;
import com.gzhryc.net_marketing.cloud.micro.api.MicroConstants;
import com.gzhryc.net_marketing.cloud.micro.api.dto.v1.DefaultResponse;
import com.gzhryc.net_marketing.cloud.micro.api.dto.v1.member.MemberAddressDelRequest;
import com.gzhryc.net_marketing.cloud.micro.api.dto.v1.member.MemberAddressInfoRequest;
import com.gzhryc.net_marketing.cloud.micro.api.dto.v1.member.MemberAddressInfoResponse;
import com.gzhryc.net_marketing.cloud.micro.api.dto.v1.member.MemberAddressListRequest;
import com.gzhryc.net_marketing.cloud.micro.api.dto.v1.member.MemberAddressListResponse;
import com.gzhryc.net_marketing.cloud.micro.api.dto.v1.member.MemberAddressSaveRequest;
import com.gzhryc.net_marketing.cloud.micro.api.dto.v1.member.MemberAddressUpdateDefRequest;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.common.CountryAreaUtils;
import com.gzhryc.shared_device.oem.code.BaseConstants;
import com.gzhryc.shared_device.oem.code.member.LoginMember;
import com.gzhryc.shared_device.oem.code.member.dao.db.MemberAddress;
import com.gzhryc.shared_device.oem.code.member.dao.enums.MemberAddressEnum;
import com.gzhryc.shared_device.oem.code.member.services.MemberAddressService;

/**
 * 会员收货地址
 */
public class MemberAddressController {

	static Logger log = Logger.getLogger(MemberAddressController.class);

	@Route(value = { "/member/address/list.data" })
	public MemberAddressListResponse addressList(HttpServletRequest request) {
		MemberAddressListResponse apiResp = new MemberAddressListResponse(ClientAPICodeConstants.NULL_DATA);
		MemberAddressListRequest apiReq = WebUtils.getJsonRequest(request, MemberAddressListRequest.class);
		if (apiReq != null && apiReq.getBody().getPageNo() != null) {
			LoginMember loginMember = WebUtils.getAttribute(request, MicroConstants.ATTR_LOGIN_MEMBER, LoginMember.class);
			if (loginMember != null) {
				List<MemberAddress> addressList = MemberAddressService.self(BaseConstants.JDBC_KEY).findByMemberId(loginMember.getId(),
						apiReq.getBody().getPageNo(), apiReq.getBody().getPageLimit());
				if (addressList != null && addressList.size() > 0) {
					apiResp.setCode(ClientAPICodeConstants.SUCCESS, null);
					for (MemberAddress mailAddress : addressList) {
						MemberAddressListResponse.Item item = new MemberAddressListResponse.Item();
						item.setId(mailAddress.getId());
						item.setReceiveName(mailAddress.getReceiveName());
						item.setReceivePhone(mailAddress.getReceivePhone());
						item.setAddress(mailAddress.getFullAddress());
						if(MemberAddressEnum.State.Default.index().equals(mailAddress.getState())){
							item.setIsDefault(true);
						}else {
							item.setIsDefault(false);
						}
						apiResp.getBody().getItems().add(item);
						if(item.getIsDefault()){
							apiResp.getBody().setDefId(item.getId());
						}
					}
				}
			}
		}else{
			apiResp.setCode(ClientAPICodeConstants.EXECUTE_ERROR, "获取收货地址失败，参数错误！");
		}
		return apiResp;
	}

	@Route(value = { "/member/address/info.data" })
	public MemberAddressInfoResponse addressInfo(HttpServletRequest request){
		MemberAddressInfoResponse apiResp = new MemberAddressInfoResponse(ClientAPICodeConstants.EXECUTE_ERROR,"获取失败，请稍后再试");
		MemberAddressInfoRequest apiReq = WebUtils.getJsonRequest(request, MemberAddressInfoRequest.class);
		LoginMember loginMember = WebUtils.getAttribute(request, MicroConstants.ATTR_LOGIN_MEMBER, LoginMember.class);
		if (loginMember != null) {
			if (apiReq != null && apiReq.getBody().getId() != null) {
				MemberAddress mailAddress = MemberAddressService.self(BaseConstants.JDBC_KEY).getByKeys(loginMember.getId(),apiReq.getBody().getId());
				if(mailAddress != null){
					apiResp.setCode(ClientAPICodeConstants.SUCCESS,null);
					apiResp.getBody().setId(mailAddress.getId());
					apiResp.getBody().setArea(mailAddress.getProvince() + "-" + mailAddress.getCity() + "-" + mailAddress.getCityArea());
					apiResp.getBody().setAddress(mailAddress.getAddress());
					apiResp.getBody().setReceiveName(mailAddress.getReceiveName());
					apiResp.getBody().setReceivePhone(mailAddress.getReceivePhone());
					if(MemberAddressEnum.State.Default.index().equals(mailAddress.getState())){
						apiResp.getBody().setIsDefault(true);
					}else {
						apiResp.getBody().setIsDefault(false);
					}
				}
			}
		}
		return apiResp;
	}

	@Route(value = { "/member/address/save.do" })
	public DefaultResponse saveAddress(HttpServletRequest request) {
		DefaultResponse apiResp = new DefaultResponse(ClientAPICodeConstants.EXECUTE_ERROR, "保存收货地址失败，请稍后再试！");
		MemberAddressSaveRequest apiReq = WebUtils.getJsonRequest(request, MemberAddressSaveRequest.class);
		if (apiReq != null) {
			LoginMember loginMember = WebUtils.getAttribute(request, MicroConstants.ATTR_LOGIN_MEMBER, LoginMember.class);
			if (loginMember == null) {
				apiResp.setMsg("登陆失效，请重新登陆");
				return apiResp;
			}
			if (StringUtils.isBlank(apiReq.getBody().getAddress())) {
				apiResp.setMsg("请填写详细地址");
				return apiResp;
			}
			if (StringUtils.isBlank(apiReq.getBody().getReceiveName())) {
				apiResp.setMsg("请填写收件人姓名");
				return apiResp;
			}
			if (StringUtils.isBlank(apiReq.getBody().getReceivePhone())) {
				apiResp.setMsg("请填写收件人电话");
				return apiResp;
			}else if(!ValidateTools.isPhone(apiReq.getBody().getReceivePhone())){
				apiResp.setMsg("请填写正确的收件人电话");
				return apiResp;
			}
			if(StringTools.isBlank(apiReq.getBody().getProvince()) || StringTools.isBlank(apiReq.getBody().getCity())
					|| StringTools.isBlank(apiReq.getBody().getCityArea())){
				apiResp.setMsg("请选择收件所在地区");
				return apiResp;
			}
			String areaCode = CountryAreaUtils.getAreaIds(new String[]{apiReq.getBody().getProvince(), apiReq.getBody().getCity(), apiReq.getBody().getCityArea()});
			if (StringUtils.isBlank(areaCode)) {
				apiResp.setMsg("请选择正确的收件所在地区");
				return apiResp;
			}

			MemberAddress mailAddress = ClassTools.copy(new MemberAddress(), apiReq.getBody());
			mailAddress.setAreaCode(areaCode);
			mailAddress.setMemberId(loginMember.getId());
			if(apiReq.getBody().getIsDefault()){
				mailAddress.setState(MemberAddressEnum.State.Default.index());
			}else{
				mailAddress.setState(MemberAddressEnum.State.Normal.index());
			}
			try {
				if (mailAddress.getId() != null) {
					if (MemberAddressService.self(BaseConstants.JDBC_KEY).updateNotNull(mailAddress)) {
						apiResp.setCode(ClientAPICodeConstants.SUCCESS, null);
					}
				} else {
					if (MemberAddressService.self(BaseConstants.JDBC_KEY).add(mailAddress)) {
						apiResp.setCode(ClientAPICodeConstants.SUCCESS, null);
					}
				}
			} catch (LogicException e) {
				apiResp.setMsg(e.getMessage());
			}
		}
		return apiResp;
	}

	@Route(value = { "/member/address/updateDef.do" })
	public DefaultResponse updateDefault(HttpServletRequest request){
		DefaultResponse apiResp = new DefaultResponse(ClientAPICodeConstants.EXECUTE_ERROR, "设置默认地址失败，请稍后再试！");
		MemberAddressUpdateDefRequest apiReq = WebUtils.getJsonRequest(request, MemberAddressUpdateDefRequest.class);
		LoginMember loginMember = WebUtils.getAttribute(request, MicroConstants.ATTR_LOGIN_MEMBER, LoginMember.class);
		if (apiReq != null && apiReq.getBody().getId() != null && loginMember != null) {
			try {
				MemberAddress mailAddress = new MemberAddress();
				mailAddress.setId(apiReq.getBody().getId());
				mailAddress.setMemberId(loginMember.getId());
				mailAddress.setState(MemberAddressEnum.State.Default.index());
				if(MemberAddressService.self(BaseConstants.JDBC_KEY).updateNotNull(mailAddress)){
					apiResp.setCode(ClientAPICodeConstants.SUCCESS,null);
				}
			} catch (LogicException e) {
				apiResp.setMsg(e.getMessage());
			}
		}
		return apiResp;
	}

	@Route(value = { "/member/address/del.do" })
	public DefaultResponse delAddress(HttpServletRequest request){
		DefaultResponse apiResp = new DefaultResponse(ClientAPICodeConstants.EXECUTE_ERROR, "删除收货地址失败，请稍后再试！");
		MemberAddressDelRequest apiReq = WebUtils.getJsonRequest(request, MemberAddressDelRequest.class);
		if (apiReq != null && apiReq.getBody().getId() != null) {
			LoginMember loginMember = WebUtils.getAttribute(request, MicroConstants.ATTR_LOGIN_MEMBER, LoginMember.class);
			if (loginMember != null) {
				MemberAddress mailAddress = MemberAddressService.self(BaseConstants.JDBC_KEY).getByKeys(loginMember.getId(),apiReq.getBody().getId());
				if(mailAddress != null){
					if (mailAddress.getMemberId().equals(loginMember.getId())) {
						try {
							if(MemberAddressService.self(BaseConstants.JDBC_KEY).deleteByKeys(loginMember.getId(),apiReq.getBody().getId())){
								apiResp.setCode(ClientAPICodeConstants.SUCCESS, null);
							}
						} catch (LogicException e) {
							apiResp.setMsg(e.getMessage());
						}
					}else{
						apiResp.setMsg("无权限删除");
					}
				}else{
					apiResp.setMsg("未获取收货地址");
				}
			}else{
				apiResp.setCode(ClientAPICodeConstants.EXECUTE_PHONE_REGISTER,"请用手机注册会员");
			}
		}
		return apiResp;
	}

}
